package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.model.dto.ReviewDTO;
import com.example.mybatisplusdemo.service.IRatingService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IReviewService;
import com.example.mybatisplusdemo.model.domain.Review;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-07-04
 * @version v1.0
 */
@RestController
@RequestMapping("/api/review")
public class ReviewController {

    private final Logger logger = LoggerFactory.getLogger( ReviewController.class );

    @Autowired
    private IReviewService reviewService;
    @Autowired
    private IRatingService ratingService;

    @GetMapping("getById")
    public JsonResponse getById(Long id) {
        Review review = reviewService.getById(id);
        return JsonResponse.success(review);
    }

    // 根据用户id分页查询评论
    @GetMapping("listReviewsByUserId")
    public JsonResponse listReviewsByUserId(PageDTO pageDTO, Long userId) {
        Page<ReviewDTO> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        page = reviewService.listReviewsByUserId(page, userId);
        return JsonResponse.success(page);
    }

    // 增加评论
    @PostMapping("addReview")
    public JsonResponse addReview(@RequestBody Review review) {
        boolean result = reviewService.save(review);
        return JsonResponse.success(result);
    }

    // 删除评论
    @GetMapping("deleteById")
    public JsonResponse deleteById(Long reviewId) {
        // 深度优先删除所有子评论
        boolean result = true;
        List<Long> childId = reviewService.listChildIdByParentId(reviewId);
        for (Long id : childId) {
            result = result && (boolean) deleteById(id).getData();
        }
        // 删除父评论
        result = result && reviewService.removeById(reviewId);
        // 删除对应评分
        ratingService.deleteByReviewId(reviewId);
        return JsonResponse.success(result);
    }

    //根据父id评论查询相应的子id评论
    @GetMapping("getChildReviewByParentId")
    public JsonResponse getChildReviewByParentId(Long parentId){
        List<ReviewDTO> reviewDTOS=reviewService.getChildReviewByParentId(parentId);
        return JsonResponse.success(reviewDTOS);
    }

    //根据店铺的id查询相应的父评论
    @GetMapping("getReviewByStoreId")
    public JsonResponse getReviewByStoreId(Long storeId) {
        List<ReviewDTO> reviewDTOs = reviewService.getReviewByStoreId(storeId);
        return JsonResponse.success(reviewDTOs);
    }

    //插入子评论
    @PostMapping("/insertChildReview")
    public JsonResponse insertReview(@RequestBody ReviewDTO reviewDTO) {
        boolean result = reviewService.insertChildReview(reviewDTO);
        return JsonResponse.success(result); //返回插入结果
    }

    //插入父评论
    @PostMapping("/insertParentReview")
    public JsonResponse insertParentReview(@RequestBody ReviewDTO reviewDTO) {
        boolean result = reviewService.insertParentReview(reviewDTO);
        return JsonResponse.success(result);
    }

    //根据用户id返回其所有的父评论
    @GetMapping("/getCurUserReviewsById")
    public JsonResponse getCurUserReviewsById(@RequestParam Long userId) {
        List<Review> parentReviews = reviewService.getParentReviewsByUserId(userId);
        return JsonResponse.success(parentReviews);
    }
}